<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<head>
  <title>Ubiquity Web Search Commands for Japanese, nora - jWikipedia</title>
</head>
<body>
<p>This xhtml source contains commands for searching the Web.  Its
corresponding feed is <a href="ubiquity-commands-ja-nora-jwikipedia.html">here</a>.</p>

<div id="jwikipedia" style="display: none;">
<b>${query}</b>に該当するWikipediaの記事:

{for article in results}
  <div style="clear: both;border:1px solid #777777; -moz-border-radius:4px;background:#333333;margin:2px 0px 0px 0px;padding:0px;">
    <div style="border:1px solid #777777; -moz-border-radius:4px;background:#666666;margin:0px;padding:0px;font-size:90%;">
        <a href="${article.title|wikilink}" accesskey="${article.key}">
        <span style="font-size:xx-small; border-width:1px; border-style:solid;-moz-border-radius:2px; padding:0px 2px 0px 2px; vertical-align:center; margin-left:1px; max-width:20px; max-height:10px; overflow:visible;float:none;background: #dddddd;color:#333333;">${article.key}</span><b>${article.title}</b>
        </a>
    </div>
    <div style="margin-left: 10px; font-size: 80%;" wikiarticle="${article.title}">
      <i>項目の詳細を取得中...</i>
    </div>
    <div style="clear:both;"> </div>
  </div>
{forelse}
  <b>該当項目が見つかりませんでした</b>
{/for}
<div style="font-size:70%;clear:both;">※[Ctrl]+[Alt]+[番号] でジャンプ</div>
</div>

<script class="commands"><![CDATA[

//
// jwikipedia search
// ja.wikipedia.org
//


CmdUtils.CreateCommand({
  //
  // about Original Source Code (Wikipedia search command)
  //
  //homepage: "http://theunfocused.net/moz/ubiquity/verbs/",
  //author: {name: "Blair McBride", email: "blair@theunfocused.net"},
  //contributors: ["Viktor Pyatkovka"],
  //license: "MPL",
  //
  name: "jwikipedia",
  synonyms: ["lookup"],
  takes: {search: noun_arb_text},
  homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
  author: {name: "hokunarin", email: "hokunarin@gmail.com"},
  license: "MPL",
  
  icon: "http://ja.wikipedia.org/favicon.ico",
  description: "Wikipedia日本語版を検索します",
  preview: function(previewBlock, directObject) {
    var apiUrl = "http://ja.wikipedia.org/w/api.php";

    var searchText = jQuery.trim(directObject.text);
    if(searchText.length < 1) {
      var previewStr = "Wikipedia日本語版検索";
      previewBlock.innerHTML = previewStr;
      return;
    }

    var previewTemplate = "<b>${query}</b> をWikipedia日本語版で検索します...";
    var previewData = {query: searchText};
    previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);

    var apiParams = {
      format: "json",
      action: "query",
      list: "search",
      srlimit: 5,
      srwhat: "text",
      srsearch: searchText
    };

    CmdUtils.previewAjax(previewBlock, {
      type: "GET",
      url: apiUrl,
      data: apiParams,
      datatype: "string",
      error: function() {
        previewBlock.innerHTML = "Error searching Wikipedia";
      },
      success: function(searchResponse) {
        searchResponse = Utils.decodeJson(searchResponse);

        if(!("query" in searchResponse && "search" in searchResponse.query)) {
          previewBlock.innerHTML = "Error searching Wikipedia";
          return;
        }

        function generateWikipediaLink(title) {
          var wikipediaUrl = "http://ja.wikipedia.org/wiki/";
          return wikipediaUrl + title.replace(/ /g, "_");
        }

	//for access keys
        for(var i=0;i<searchResponse.query.search.length;i++){
          searchResponse.query.search[i].key = i+1;
        }

        previewData = {
          query: searchText,
          results: searchResponse.query.search,
          _MODIFIERS: {wikilink: generateWikipediaLink}
        };

        previewBlock.innerHTML = CmdUtils.renderTemplate(
          jQuery("#jwikipedia", feed.dom).html(),
          previewData);

        jQuery(previewBlock).find("div[wikiarticle]").each(function() {
          var article = jQuery(this).attr("wikiarticle");
          fetchWikipediaArticle(this, article);
        });

      }
    });
  },
  execute: function(directObject) {
    var searchUrl = "http://ja.wikipedia.org/wiki/Special:Search";
    var searchParams = {search: directObject.text};
    Utils.openUrlInBrowser(searchUrl + Utils.paramsToString(searchParams));
  }
});

function fetchWikipediaArticle(previewBlock, articleTitle) {
  /* TODO
   - apply CSS max-height & overflow-y to summary container
  */
  var apiUrl = "http://ja.wikipedia.org/w/api.php";
  var apiParams = {
    format: "json",
    action: "parse",
    page: articleTitle
  };

  CmdUtils.previewAjax(previewBlock, {
    type: "GET",
    url: apiUrl,
    data: apiParams,
    datatype: "string",
    error: function() {
      previewBlock.innerHTML = "<i>概要の取得失敗</i>";
    },
    success: function(responseData) {
      responseData = Utils.decodeJson(responseData);

      var tempElement = previewBlock.ownerDocument.createElementNS("http://www.w3.org/1999/xhtml", "div");
      tempElement.innerHTML = responseData.parse.text["*"];

      var articleSummary = jQuery(tempElement).find('p').eq(0).text();

      articleSummary = articleSummary.replace(/\[([^\]]+)\]/g,"");

      var articleImageSrc = jQuery(tempElement).find(".infobox img").attr("src") ||
      jQuery(tempElement).find(".thumbimage").attr("src");

      var previewTemplate = "<img src=\"${img}\" style=\"float: left; max-width: 80px; max-height: 80px; background-color: white; margin: 1px 1px 1px 1px;\" />" +
      "<span class=\"wikisummary\">${summary}</span>";

      var previewData = {
        img: articleImageSrc,
        summary: articleSummary
      };

      previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
    }
  });
}



]]></script>
</body>
</html>
